//全局属性配置，在任意组件内可以使用this.$http获取axios对象
Vue.prototype.$http = axios
axios.defaults.baseURL='http://localhost:8080'

function getUrlParam() {
        var url = location.search; //获取url中"?"符后的字串
        var theRequest = {};
        if (url.indexOf("?") != -1) {
                 var str = url.substr(1);
                 strs = str.split("&");
                 for (var i = 0; i < strs.length; i++) {
                         theRequest[strs[i].split("=")[0]] = decodeURIComponent(strs[i].split("=")[1]);
                    }
             }
         return theRequest;
}


//前置拦截 请求拦截
axios.interceptors.request.use(
    config => {
        const token = localStorage.getItem('token')
        if (token ) { // 判断是否存在token
            //请求之前添加
            config.headers['U-TOKEN'] = token
        }
        return config
    },
    err => {
        return Promise.reject(err)
    })
//后置拦截器  响应拦截
axios.interceptors.response.use(
    config => {
      //config data: {message: "noLogin",success: false}
        if(!config.data.success && config.data.message==="noLogin"){
            //跳转到登录页面
            location.href="/login.html"
        }
        return config
    },
    err => {
        return Promise.reject(err)
    })


